package org.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.easybuy.entity.User;
import org.easybuy.service.UserService;
import org.easybuy.serviceimpol.UserServiceImpol;

/**
 * 
 * @userServlet
 * 
 * */
public class UserServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	String hint;
	String status;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		UserService userService = new UserServiceImpol();
		String opr = request.getParameter("opr");
		PrintWriter out = response.getWriter();
		if ("userName".equals(opr)) {
			String user = request.getParameter("l_user");
			User user2 = userService.getAllLoginName(user);
			if (user2 == null) {
				status = "success";
				hint = "用户名可以注册";
			} else {
				status = "error";
				hint = "用户名已注册过";
			}
			out.print("[{\"hint\":\"" + hint + "\",\"status\":\"" + status
					+ "\"}]");
			out.flush();
			out.close();
		} else if ("pwd".equals(opr)) {
			String user = request.getParameter("l_user");
			User user2 = userService.getAllLoginName(user);
			if (user2 == null) {
				status = "error";
				hint = "用户名不存在";
			} else {
				status = "success";
				hint = "用户名可用";
			}
			out.print("[{\"hint\":\"" + hint + "\",\"status\":\"" + status
					+ "\"}]");
			out.flush();
			out.close();
		} else if ("pwd1".equals(opr)) {
			String pwd2 = request.getParameter("l_pwd");
			String user = request.getParameter("l_user");
			session.setAttribute("name", user);
			User user2 = userService.getUserPassword(user, pwd2);
			if (user2 != null) {
				session.setAttribute("SHOW_USERNAME", user);
				status = "success";
				hint = user;
			} else {
				status = "error";
				hint = "密码不正确";

			}
			out.print("[{\"hint\":\"" + hint + "\",\"status\":\"" + status
					+ "\"}]");
			out.flush();
			out.close();

		}

	}

}
